import path from 'path'
import { defineConfig } from 'vite'
import { out_dir, port } from './config'
import vue from '@vitejs/plugin-vue'
import vitePluginCdn from 'vite-plugin-cdn'

export default ({ mode }) =>
  defineConfig({
    esbuild: {
      jsxFactory: 'h',
      jsxFragment: 'Fragment',
      jsxInject: `import { h } from 'vue'`,
    },
    resolve: {
      alias: [{ find: '@', replacement: path.resolve(__dirname, 'src') }],
    },
    server: {
      hostname: '0.0.0.0',
      port,
      open: false,
      https: false,
      ssr: false,
      base: './',
      outDir: out_dir,
      proxy: {
        // 本地开发环境通过代理实现跨域，生产环境使用 nginx 转发
        '/api': {
          target: 'http://127.0.0.1:7001', // 后端服务实际地址
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
    plugins: [
      vue(),
      vitePluginCdn({
        esm: true,
        modules: [
          {
            name: 'vue',
            url: '',
          },
        ],
      }),
    ],
    build: {
      sourcemap: true,
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
        },
      },
    },
  })
